/*
@title: sPaint!
@description: sPaint! is a sandbox-style game where players can create designs by placing and erasing pixels on a grid.
@author: yzde
@tags: ['sandbox']
@addedOn: 2023-02-02

controls:
use w (at the first screen) to start
use w,a,s,d to move the cursor or the eraser
use k to place or erase pixel
use l to reset everything
use j to change between cursor and eraser

coming soon:
adding colors! :p

socials:
twitter: @yazidears
ig: @yazidears
tiktok: @yazidears
slack: yzde or yazide
 <3 by yazide and from the lorx team!
*/
let cursorx
let cursory

const cursor = "c";
const pixel = "w";
const eraser = "e";
const transpixel = "t";
let start = "False";
let mode = "place";
addText(" press\n   w\nto play", { x: 6, y: 12, color: color`0`})
setLegend(
  [ cursor, bitmap`
0000000000000000
00............00
0.0..........0.0
0..0........0..0
0...0......0...0
0....0....0....0
0.....0000.....0
0.....0..0.....0
0.....0..0.....0
0.....0000.....0
0....0....0....0
0...0......0...0
0..0........0..0
0.0..........0.0
00............00
0000000000000000`],
  [ pixel, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
  [ eraser, bitmap`
0000000000000000
0000000000000000
0222222222222220
02............20
02............20
02.9999999999.20
02.9.....9229.20
02.9.99.99229.20
02.99.99.9229.20
02.9.....9229.20
02.9999999999.20
02............20
02............20
0222222222222220
0000000000000000
0000000000000000`],
  [ transpixel, bitmap`
................
................
................
................
................
................
................
................
................
................
................
................
................
................
................
................`]
);

let level = 0;
const levels = [
  map`
.....................................
.....................................
.....................................
.....................................
.....................................
.....................................
..............................wwww...
.........wwwww................wwww...
.........w...w................wwww...
.........w...w................wwww...
....wwww.wwwww................www....
....w....wwwww................www....
....w....w....................www....
....wwww.w..wwww.www.w..w.www.www....
.......w.w..w..w..w..ww.w..w..www....
.......w.w..wwww..w..w.ww..w.........
....wwww.w..w..w.www.w..w..w..www....
..............................www....
..............................www....
.....................................
.....................................
.....................................
.....................................
.....................................
............t........................
...........t.ttttttt.t.t.tttt........
...........t..............ttt........
..........................ttt........
............t....t......tttt.........
.....................................
.....................................
.....................................
.....................................
.....................................
.....................................
.....................................
.....................................
.....................................
.....................................`,
  map`
.................................
.................................
.................................
....wwwwwwwwwwwwwwww.............
....w.....w...ww...w.wwwww.wwww..
....w.....ww..ww...w.w...w.w..w..
....wwwww.wwwwwwwwwwww...wwwwww..
....w.....www..www..w....wwww....
....w.....w.ww.w.ww.w....wwwwww..
....w.....w..www..www....w.w..w..
....wwwww.w...ww...ww...ww.w..w..
...............w.....wwww..w.....
...........................w.....
...................ww............
............wwwwwwwww............
............w...ww.wwww..........
............w...ww..w............
............w...ww..w............
............w...ww..w............
............wwwwwwwww............
...............ww................
.................................
.................................
.................................
.................................
.................................
.................................
.................................
.................................
.................................
.................................
.................................
.................................
.................................
.................................
.................................
.................................
.wwwwww..........................
.w..w..w.........................
.ww.w..w.........................
.w..www..........................
.wwww.w..........................
.......w.........................
.................................`,
  map`
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
...........................
..c........................
...........................
...........................`
];
function delay(time) {
  return new Promise(resolve => setTimeout(resolve, time));
}

const currentLevel = levels[level];
setMap(levels[0]);

//setSolids([ pixel, cursor, eraser ]);

// START - PLAYER MOVEMENT CONTROLS

onInput("s", () => {
  if (mode === "place") {
  if(!getFirst(cursor)) return;
  getFirst(cursor).y += 1;
  }
  else{
  if(!getFirst(eraser)) return;
    getFirst(eraser).y += 1;
  }
});

onInput("d", () => {
  
  if (mode === "place") {
    if(!getFirst(cursor)) return;
  getFirst(cursor).x += 1;
  }
  else{
    if(!getFirst(eraser)) return;
    getFirst(eraser).x += 1;
  }
});
onInput("a", () => {
  if (mode === "place") {
    if(!getFirst(cursor)) return;
  getFirst(cursor).x -= 1;
  }
  else{
    if(!getFirst(eraser)) return;
    getFirst(eraser).x -= 1;
  }
});
onInput("k", () => {
  if (mode === "place"){
  if(!getFirst(cursor)) return;
    cursorx = getFirst(cursor).x
    if(!getFirst(cursor)) return;
    cursory = getFirst(cursor).y
    addSprite((cursorx), (cursory), pixel)
  }
  else if (mode === "erase"){
     if(!getFirst(eraser)) return;
    cursorx = getFirst(eraser).x
     if(!getFirst(eraser)) return;
    cursory = getFirst(eraser).y
    clearTile(cursorx,cursory)
    addSprite(cursorx, cursory, eraser)
  }
});
onInput("w", () => {
  if (start === "False"){
    level = 2
    clearText()
    setMap(levels[2]);
    start = "True"
  } else if (mode === "place") {
     if(!getFirst(cursor)) return;
  getFirst(cursor).y -= 1;
  }
  else{
     if(!getFirst(eraser)) return;
    getFirst(eraser).y -= 1;
  } 
});
// END - PLAYER MOVEMENT CONTROLS

onInput("j", () => {
  
    if (mode === "place"){
      mode = "erase"
       if(!getFirst(cursor)) return;
      cursorx = getFirst(cursor).x
       if(!getFirst(cursor)) return;
      cursory = getFirst(cursor).y
      clearTile(cursorx, cursory)
      addSprite(cursorx, cursory, eraser)
      addText("erase mode", { 
  x: 5,
  y: 4,
  color: color`3`
})
      delay(1000).then(() => clearText());
      
    }
    else if (mode === "erase"){
      mode = "place"
       if(!getFirst(eraser)) return;
      cursorx = getFirst(eraser).x
       if(!getFirst(eraser)) return;
      cursory = getFirst(eraser).y
      clearTile(cursorx, cursory)
      addSprite(cursorx, cursory, cursor)
      addText("place mode", { 
  x: 5,
  y: 4,
  color: color`0`
})
      delay(1000).then(() => clearText());
      
    
    }
      
  
});
onInput("l", () => {
  const currentLevel = levels[level];
  if (currentLevel !== undefined) {
    clearText("");
    setMap(currentLevel);
  }
});
